package com.example.home.pet.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.pet.activity.DoctorInquiryDetail;
import com.example.home.pet.bean.DoctorListBean;
import com.google.gson.Gson;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class DoctorListAdapter extends RecyclerView.Adapter<DoctorListAdapter.MyHoldy> {
    private Context context;
    private List<DoctorListBean.RowsBean> list;

    public DoctorListAdapter(Context context, List<DoctorListBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充视图
        View view = LayoutInflater.from(context).inflate(R.layout.item_doct, null);
        return new DoctorListAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        DoctorListBean.RowsBean rowsBean = list.get(position);
        Glide.with(context).load(ip+rowsBean.getAvatar()).into(holder.avatar);
        holder.name.setText(rowsBean.getName());
        holder.jobName.setText("职称："+rowsBean.getJobName());
        holder.workingYears.setText("从业年限："+rowsBean.getWorkingYears()+"年");
        holder.practiceNo.setText("执业编号："+rowsBean.getPracticeNo());


        String goodAt = rowsBean.getGoodAt();
        if (goodAt.length() >=15){
            holder.goodAt.setText("擅长："+goodAt.substring(0,15)+"...");
        }else {
            holder.goodAt.setText(goodAt);
        }
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DoctorInquiryDetail.class);
                Gson gson = new Gson();
                intent.putExtra("DoctorListBean",gson.toJson(list.get(position)));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private ImageView avatar;
        private TextView name;
        private TextView jobName;
        private TextView workingYears;
        private TextView practiceNo;
        private TextView goodAt;
        private RelativeLayout rootView;


        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.avatar);
            name = itemView.findViewById(R.id.name);
            jobName = itemView.findViewById(R.id.jobName);
            workingYears = itemView.findViewById(R.id.workingYears);
            practiceNo = itemView.findViewById(R.id.practiceNo);
            goodAt = itemView.findViewById(R.id.goodAt);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
